package de.tabuma.sso.shopping;

import de.tabuma.sso.*;
import de.tabuma.shopping.entity.LicenseType;
import de.tabuma.shopping.logik.LicensePackageFacade;
import de.tabuma.sso.group.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.*;
import java.util.logging.*;
import de.tabuma.shopping.controller.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.2.2.1.2.1 $
 * @since $Date: 2010/01/13 10:23:10 $
 */
public class ShoppingController extends controller {

    private static final Logger log = Logger.getLogger(ShoppingController.class.getName());

    ShoppingFacade shoppingFacade;
    @Override
    public LicensePackageFacade getLicensePackageFacade() {
        return shoppingFacade;
    }

    @Override
    public String getUrl() {
        return "http://localhost:8081/";
    }

    String orderNav;
    @Override
    public String getOrderNav() {
        return orderNav;
    }

    @Override
    public Class getAssignmentClass() {
        return de.tabuma.sso.group.Group.class;
    }

    @Override
    public List getAssignmentList() {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        List gList = GroupFacade.getList(user.getEmail());
        return gList;
    }

    @Override
    public Field getAssignmentKeyField() {
        try {
            return Group.class.getDeclaredField("id");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return null;
    }

    @Override
    public Field getAssignmentShowField() {
        try {
            return Group.class.getDeclaredField("name");
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return null;
    }

    @Override
    public String getAssignmentObjectname() {
        return "Group";
    }
    
    @Override
    public List<LicenseType> getLicenseTypeList() {
        List<LicenseType> list = new ArrayList<LicenseType>();
        LicenseType license = new LicenseType();
        license.setId(de.tabuma.sso.constants.SSO_SOAP_LICENSE_TYP);
        license.setName("SOAP License");
        list.add(license);
        return list;
    }

    @Override
    public Hashtable<Integer, String> getMethodHashtable() {
        Hashtable<Integer,String> methodHash = new Hashtable<Integer,String>();

        //methods for customer manipulation
        methodHash.put(constants.CUSTOMER_CREATE, "create customer");
        methodHash.put(constants.CUSTOMER_GET, "get customer");
        methodHash.put(constants.CUSTOMER_GETLIST, "get customer List");
        methodHash.put(constants.CUSTOMER_REMOVE, "remove customer");
        methodHash.put(constants.CUSTOMER_UPDATE, "update customer");
        methodHash.put(constants.CUSTOMER_CREATE, "create customer for google user");

        //methods for groupprivileg manipulation
        methodHash.put(constants.GROUPPRIVILEG_CREATE, "create group privileg");
        methodHash.put(constants.GROUPPRIVILEG_GET, "get group privileg");
        methodHash.put(constants.GROUPPRIVILEG_UPDATE, "update group privileg");
        methodHash.put(constants.GROUPPRIVILEG_GETLIST1, "list group privileg (with group id)");
        methodHash.put(constants.GROUPPRIVILEG_GETLIST2, "list group privileg (with privileg id)");
        methodHash.put(constants.GROUPPRIVILEG_REMOVE, "remove group privileg");

        //methods for group manipulation
        methodHash.put(constants.GROUP_CREATE, "create group");
        methodHash.put(constants.GROUP_GET, "get group");
        methodHash.put(constants.GROUP_GETLIST, "get group list");
        methodHash.put(constants.GROUP_REMOVE, "remove group");
        methodHash.put(constants.GROUP_UPDATE, "update group");

        //methods for privileg manipulation
        methodHash.put(constants.PRIVILEG_CREATE, "create privileg");
        methodHash.put(constants.PRIVILEG_GET, "get privileg");
        methodHash.put(constants.PRIVILEG_GETLIST, "get privileg");
        methodHash.put(constants.PRIVILEG_REMOVE, "remove privileg");
        methodHash.put(constants.PRIVILEG_UPDATE, "update privileg");

        //methods for project manipulation
        methodHash.put(constants.PROJECT_CREATE, "create project");
        methodHash.put(constants.PROJECT_GET, "get project");
        methodHash.put(constants.PROJECT_GETLIST, "get project list");
        methodHash.put(constants.PROJECT_REMOVE, "remove project");
        methodHash.put(constants.PROJECT_UPDATE, "update project");

        //methods for sso manipultation
        methodHash.put(constants.SSO_GETSSO, "get sso");
        methodHash.put(constants.SSO_SESSION_IS_ACTIV, "session is activ");
        methodHash.put(constants.SSO_USERISPROJECTADAMIN, "user is admin");
        methodHash.put(constants.SSO_GETUSERPRIVILEGES, "privileges for user");

        //methods for usergroup manipulation
        methodHash.put(constants.USERGROUP_CREATE, "create usergroup");
        methodHash.put(constants.USERGROUP_GET, "get usergroup");
        methodHash.put(constants.USERGROUP_GETLIST, "get usergroup list");
        methodHash.put(constants.USERGROUP_REMOVE, "remove usergroup");
        methodHash.put(constants.USERGROUP_UPDATE, "update usergroup");

        //methods for
        methodHash.put(constants.USER_CREATE, "create user");
        methodHash.put(constants.USER_GET, "get user");
        methodHash.put(constants.USER_REMOVE, "remove user");
        methodHash.put(constants.USER_UPDATE, "update user");
        methodHash.put(constants.USER_CREATE1, "create user for google user");
        return methodHash;
    }
    
    public ShoppingController(String servlet, Hashtable parameter, String orderNav) {
        super(servlet, parameter);
        shoppingFacade = new ShoppingFacade();
        this.orderNav = orderNav;
    }

    @Override
    public void navigate(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {
        super.navigate(context, request, response, out);
    }

    @Override
    public String getValueFromProperties(String key) {
        return java.util.ResourceBundle.getBundle("de.tabuma.sso.shopping.language").getString(key);
    }

}

